package com.zh.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Tables;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.uuid.UUID;
import com.zh.mapper.CourseDao;
import com.zh.mapper.PersonDao;
import com.zh.po.CoursePo;
import com.zh.po.PersonPo;
import lombok.SneakyThrows;
import org.apache.commons.lang3.text.WordUtils;
import org.ddr.poi.html.HtmlRenderPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestService {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    public CourseDao courseDao;


    //todo 如果用若依的多数据源方式,则需要将查询/处理的一次操作单独用@DataSource进行标记
    @Resource
    public PersonDao personDao;

    @Resource
    public  TestService2 testService2;

    public void test(){

        // 这样调用,切面方法不生效的,相当于自己调用自己,走的是 this.xxx 是原生对象.
        testDataChange();
        testDataChange2();

        // 原因跟@Transaction 跟@async 一样,走this.xxx跟走spring包装的bean的完全不一样,代理bean拥有额外的功能.

        testService2. testDataChange2();
        testService2. testDataChange();

    }

    @DataSource(DataSourceType.SLAVE)
    public void testDataChange(){

        System.out.println(DynamicDataSourceContextHolder.getDataSourceType());
        PersonPo po = new PersonPo();
        po.setId(UUID.randomUUID().toString());
        po.setName("张辉1");
        po.setSex(1);
        personDao.insert(po);
    }
    @DataSource(DataSourceType.MASTER)
    public void  testDataChange2(){
        System.out.println(DynamicDataSourceContextHolder.getDataSourceType());
        QueryWrapper<CoursePo> cQuery = new QueryWrapper();
        cQuery.lambda().eq(CoursePo::getCid,"01");
        CoursePo coursePo = courseDao.selectOne(cQuery);
        logger.info("当前课程{}",coursePo.getCname());
    }



}
